package com.handinfo.android.ui.window;

import java.util.Timer;
import java.util.TimerTask;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.game.Role;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWCheckbox;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIHook implements IUIWindows{

	private boolean flag = false;
	public static boolean s_autoHook = false;//临时
	public static boolean s_autoRepair = false;
	public static int s_autoRepairTime = DWGameManager.FRAME_PER_SECOND * 60;
	public DWFrame m_hook_frame = null;
	private DWTitle m_hook_title = null;
	private DWImageBox m_imageBox_title = null;
	private Bitmap m_bm_title = null;
	private DWImageBox m_hook_close = null;
	private DWBackground m_hook_background = null;
	private DWBackground m_background_top = null;
	private DWBackground m_background_botton = null;
	private Bitmap m_bm_background = null;
	private Bitmap m_bm_close = null;
	private DWTextbox m_textBox_top = null;
	private DWTextbox m_textBox_time = null;
	private DWTextbox m_textBox_bottom = null;
	public DWButton m_button_top = null;
	private DWButton m_button_bottom = null;
	private Bitmap m_bm_button01 = null;
	private Bitmap m_bm_buttonDown = null;
	private DWTextbox m_textBox_shuoming = null;
	private DWTextbox m_textBox_xuechi = null;
	private static String m_str_xuechi = "挂机时血量降低，会自动帮您使用快捷栏中的补血药品。请随时关注快捷栏中药品的数量。";
	private String m_str_top = "自动攻击周围的怪物，自动使用恢复品，自动修理装备。";
	private String m_str_bottom = "挂机血池";
	private String m_str_time = "0/2000";
	private long m_system_time =5400000;
	private Timer m_time = null;
	private TimerTask m_timeTask = null;
	
	private DWCheckbox m_hook_vip = null;
	private DWTextbox m_text_vip = null;
	private final static String m_text_vipStr = "自动修装备（需VIP2）";
	
	public int m_posx;
	public int m_posy;
	
	
	private void hook(){
//		m_system_time = ;
		m_hook_frame = new DWFrame(DWFrame.FRAME_TYPE_HALF_LEFT);
		m_hook_frame.setClickClose(false);
		m_hook_title = new DWTitle("", m_hook_frame);
		m_imageBox_title = new DWImageBox(m_bm_title);
		m_imageBox_title.setNearAnchor(m_hook_title, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		//关闭按钮
		m_hook_close = new DWImageBox(m_bm_close);
		m_hook_close.setTouchZoomIn(30, 30);
		m_hook_close.addListener(m_close);
		m_hook_close.setNearAnchor(m_hook_title, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, 0, 0);
		//背景
		m_hook_background = new DWBackground(340, 380);
		if(DWGameManager.AdpaterType == 2){
			m_hook_background.setShowWideHigh(DWGameManager.Screen_Width*34/80, DWGameManager.Screen_Height*38/48);
		}
		m_hook_background.setShow(false);
		m_hook_background.setNearAnchor(m_hook_title, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 40);
		
		m_background_top = new DWBackground(350, 200);
		if(DWGameManager.AdpaterType == 2){
			m_background_top.setShowWideHigh(DWGameManager.Screen_Width*35/80, DWGameManager.Screen_Height*20/48);
		}
		m_background_top.setFill(true);
		if(DWGameManager.AdpaterType == 2){
			m_background_top.setNearAnchor(m_hook_background, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 0);
		}else{
			m_background_top.setNearAnchor(m_hook_background, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
		}
		
		m_background_botton = new DWBackground(350, 160);
		if(DWGameManager.AdpaterType == 2){
			m_background_botton.setShowWideHigh(DWGameManager.Screen_Width*35/80, DWGameManager.Screen_Height*16/48);
		}
		m_background_botton.setFill(true);
		if(DWGameManager.AdpaterType == 2){
			m_background_botton.setNearAnchor(m_background_top, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 0);
		}else{
			m_background_botton.setNearAnchor(m_background_top, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 180);
		}

		
		//提示
		if(DWGameManager.AdpaterType == 2){
			m_textBox_top = new DWTextbox(m_str_top, DWGameManager.Screen_Width*35/80 - 50, DWGameManager.Screen_Height*6/48);
		}else{
			m_textBox_top = new DWTextbox(m_str_top, 300, 60);
		}

		m_textBox_top.setNearAnchor(m_hook_background, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 20);
		
		String hook = s_autoHook?"停止挂机":"开始挂机";
		m_button_top = new DWButton(hook, m_bm_button01);
		m_button_top.setDownImage(m_bm_buttonDown);
		m_button_top.addListener(m_guaji);
		m_button_top.setNearAnchor(m_textBox_top, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 10);
		
		//勾选提示\
		m_text_vip = new DWTextbox(m_text_vipStr);
		m_text_vip.setNearAnchor(m_button_top, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 20);
		
		
		m_hook_vip = new DWCheckbox();
		m_hook_vip.addListener(m_check);
		m_hook_vip.setNearAnchor(m_text_vip, Tools.RIGHT_TOP, Tools.LEFT_TOP, 0, 0);
		
		m_hook_vip.setChecked(s_autoRepair);
		if(DWGameManager.getInstance().m_role.m_vip_lv < 2){
			m_hook_vip.setTouch(false);
			m_hook_vip.setChecked(false);
			m_text_vip.setText("<mark c=818080>"+m_text_vipStr+"</mark>");			
		}else{
			m_hook_vip.setTouch(true);
		}
		
		m_textBox_bottom = new DWTextbox(m_str_bottom, 300);
		m_textBox_bottom.setShow(false);
		m_textBox_bottom.setTouch(false);
		m_textBox_bottom.setNearAnchor(m_background_botton, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 10);
		
		m_textBox_time = new DWTextbox("0/2000", 300);//血池的显示
		m_textBox_time.setShow(false);
		m_textBox_time.setTouch(false);
		m_textBox_time.setNearAnchor(m_textBox_bottom, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0	, 0);
		
		m_button_bottom = new DWButton("购买生命", m_bm_button01);
		m_button_bottom.setShow(false);
		m_button_bottom.setTouch(false);
		m_button_bottom.setDownImage(m_bm_buttonDown);
		m_button_bottom.addListener(m_goumai);
		m_button_bottom.setNearAnchor(m_textBox_time, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 90, 50);
		
		m_textBox_shuoming = new DWTextbox("挂机中被怪攻击，优先扣除血池中的生命值。",300);
		m_textBox_shuoming.setShow(false);
		m_textBox_shuoming.setTouch(false);
		m_textBox_shuoming.setNearAnchor(m_button_bottom, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, -90, 10);
		
		if(DWGameManager.AdpaterType == 2){
			m_textBox_xuechi = new DWTextbox("<mark c=FFBB11>"+m_str_xuechi+"</mark>",DWGameManager.Screen_Width*35/80 - 50);
		}else{
			m_textBox_xuechi = new DWTextbox("<mark c=FFBB11>"+m_str_xuechi+"</mark>",300);
		}
		m_textBox_xuechi.setNearAnchor(m_background_botton, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		
		m_hook_frame.addControl(m_hook_background);
		m_hook_frame.addControl(m_background_top);
		m_hook_frame.addControl(m_background_botton);
		m_hook_frame.addControl(m_hook_title);
		m_hook_frame.addControl(m_hook_close);
		m_hook_frame.addControl(m_textBox_top);
		m_hook_frame.addControl(m_textBox_bottom);
		m_hook_frame.addControl(m_textBox_time);
		m_hook_frame.addControl(m_button_bottom);
		m_hook_frame.addControl(m_textBox_shuoming);
		m_hook_frame.addControl(m_button_top);
		m_hook_frame.addControl(m_imageBox_title);
		m_hook_frame.addControl(m_hook_vip);
		m_hook_frame.addControl(m_text_vip);
		
		m_hook_frame.addControl(m_textBox_xuechi);
		DWControlsManager.getInstance().addControl(m_hook_frame);		
	}
	//关闭
	private DWListener m_close = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			UIWindows.getInstance().m_hook.close((byte)0);
		}
	};
	//自动挂机
	private DWListener m_guaji = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			s_autoHook = s_autoHook?false:true;
			Role role = DWGameManager.getInstance().m_role;
			if(!s_autoHook){
				m_button_top.setName("开始挂机");
//				ding();
                role.initAutoAttack(null);
			}else{
				m_button_top.setName("停止挂机");
//				m_timeTask.cancel();
				setPosXY(role.m_posX, role.m_posY);
			}
		}
	};
	//自动修理
	private DWListener m_check = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			//当前未被勾选
			s_autoRepair = m_hook_vip.getChecked()?true:false;
		}
	};
	//购买]
	private DWListener m_goumai = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
//			UIWindows.getInstance().m_hook.open((byte)1);
			DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B,
					"暂不开放！");
		}
	};
	
	private void ding(){
		m_timeTask = new TimerTask() {
			
			@Override
			public void run() {
				m_system_time -= 1000;
				m_textBox_time.setText(m_str_time+""+m_system_time/3600000+":"+(m_system_time%3600000)/60000);
			}
		}; 
		
		m_time = new Timer();
		m_time.schedule(m_timeTask, 0, 1000);
	}
	
	public void init() {
		m_bm_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		m_bm_button01 = UIWindows.createImage("/img/newui/anniu_5.gnp");
		m_bm_buttonDown = UIWindows.createImage("/img/newui/anniu_5ax.gnp");
		m_bm_title = UIWindows.createImage("/img/newui/zidongguaji_1.gnp");
	}
	
	public void setPosXY(int posx,int posy){
		m_posx = posx;
		m_posy = posy;
	}
	
	public boolean checkPosXY(int posx,int posy){
		boolean flag = false;
		if(posx == m_posx && posy == m_posy){
			flag = true;
		}
		return flag;
	}

	public void open(byte index) {
		if(index == 0){
			if(flag == false){
				hook();
				flag = true;
			}else{
				if(m_hook_frame != null){
					DWControlsManager.getInstance().addControl(m_hook_frame);
				}else{
					return;
				}
			}
		}else if(index == 1){
			buyItem();
		}
	
	}

	public void close(byte index) {
		
		if(index == 0){
			flag = false;
			DWControlsManager.getInstance().removeControl(m_hook_frame);
		}else if(index == 1){
			DWControlsManager.getInstance().removeControl(m_buy_frame);
		}
	}
	
	public static void closeHook(){
		if(s_autoHook){			
			s_autoHook = false;
			s_autoRepair = false;
			s_autoRepairTime = 0;
			if(UIWindows.getInstance().m_hook != null
				&&UIWindows.getInstance().m_hook.m_button_top != null){
				UIWindows.getInstance().m_hook.m_button_top.setName("开始挂机");
			}
			if (!DWGameManager.getInstance().m_role.m_taskOpen) {
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "自动挂机被取消！");
			}
		}
	}
	
	/*************************************************购买界面*************************************/
	private DWFrame m_buy_frame = null;
	private DWTitle m_buy_title = null;
	private DWBackground m_buy_backGround = null;
	private DWImageBox m_buy_close = null;
	private DWTextbox m_textBox_name = null;
	private DWTextbox m_textBox_num = null;
	private DWTextbox m_textBox_huode = null;
	private DWTextbox m_textBox_money = null;
	private DWButton m_button_ok = null;
	
	private void buyItem(){
		m_buy_frame = new DWFrame(DWFrame.FRAME_TYPE_HALF_HCENTER, true);
		m_buy_title = new DWTitle("购买", m_buy_frame);
		
		m_buy_close = new DWImageBox(m_bm_close);
		m_buy_close.setTouchZoomIn(20, 20);
		m_buy_close.addListener(m_close_goumai);
		m_buy_close.setNearAnchor(m_buy_title, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 0, 0);
		
		m_buy_backGround = new DWBackground(m_buy_frame.getShowWidth() - 50, m_buy_frame.getShowHeight() - 50);
		m_buy_backGround.setNearAnchor(m_buy_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		
		m_textBox_name = new DWTextbox("挂机血池");
		m_textBox_name.setNearAnchor(m_buy_backGround, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 10);
		
		m_textBox_num = new DWTextbox("今日可购买"+3+"次");
		m_textBox_num.setNearAnchor(m_textBox_name, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
		
		m_textBox_huode = new DWTextbox("购买后为血池增加"+10000+"血");
		m_textBox_huode.setNearAnchor(m_textBox_name, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 50);
		
		m_textBox_money = new DWTextbox("需要花费"+5+"元宝");
		m_textBox_money.setNearAnchor(m_textBox_huode, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 50);
		
		m_button_ok = new DWButton("购买", m_bm_button01);
		m_button_ok.addListener(m_buy);
		m_button_ok.setDownImage(m_bm_buttonDown);
		m_button_ok.setNearAnchor(m_textBox_money, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 50);
		
		m_buy_frame.addControl(m_buy_backGround);
		m_buy_frame.addControl(m_buy_title);
		m_buy_frame.addControl(m_buy_close);
		m_buy_frame.addControl(m_textBox_name);
		m_buy_frame.addControl(m_textBox_num);
		m_buy_frame.addControl(m_textBox_huode);
		m_buy_frame.addControl(m_textBox_money);
		m_buy_frame.addControl(m_button_ok);
		DWControlsManager.getInstance().addControl(m_buy_frame);
		
	}
	//关闭购买
	private DWListener m_close_goumai = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			UIWindows.getInstance().m_hook.close((byte)1);
		}
	};
	//确认购买
	private DWListener m_buy = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B,
					"暂不开放！");
		}
	};
	/*
	 * 挂机按钮
	 */
	public DWButton hookButton(){
		if(DWControlsManager.getInstance().contains(m_hook_frame) == false){
			return null;
		}else
			return m_button_top;
	}
	
	/*
	 * 双倍经验按钮
	 */
	public DWButton experienceButton(){
		if(DWControlsManager.getInstance().contains(m_hook_frame) == false){
			return null;
		}else
			return m_button_bottom;
	}
	/*
	 * 关闭按钮
	 */
	public DWImageBox closeButton(){
		if(DWControlsManager.getInstance().contains(m_hook_frame) == false){
			return null;
		}else
			return m_hook_close;
	}
	
	public void dispose() {
	}

	public void logic() {
	}

}
